package com.gitee.huanminabc.jlambda_sql.jdbc;

import com.alibaba.fastjson.JSON;
import com.gitee.huanminabc.jcommon.fastjson.FastJsonUtil;
import com.gitee.huanminabc.jcommon.model.DataKeyValue;
import com.gitee.huanminabc.jcommon.reflect.AnnotationUtil;
import com.gitee.huanminabc.jlambda_sql.annotation.TableField;
import com.gitee.huanminabc.jlambda_sql.annotation.TableId;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ResultSet 工具类
 *
 * @Author: huanmin
 * @Date: 2025/4/5 11:16
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
public class ResultSetUtil {


    //将结果集转为实体类
    public static <T> T getObject(ResultSet rs, Class<T> tClass) throws SQLException {
        boolean next = rs.next();
        if (!next){
            return null;
        }
        return getMapToEntity(rs,tClass);
    }

    //将多行结果集转为实体类
    public static <T> List<T> getObjectList(ResultSet rs, Class<T> tClass) throws SQLException {
        List<T> list = new ArrayList<>();
        try {
            while (rs.next()) {
                list.add( getMapToEntity(rs,tClass));
            }
        } finally {
            rs.close();
        }
        return list;
    }

    //将结果集转为Map
    public static Map<String, Object> getMap(ResultSet rs) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        try {
            while (rs.next()) {
                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                    map.put(rs.getMetaData().getColumnName(i), rs.getObject(i));
                }
            }
        } finally {
            rs.close();
        }
        return map;
    }

    //将多行结果集转为List<Map>
    public static List<Map<String, Object>> getListMap(ResultSet rs) throws SQLException {
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            while (rs.next()) {
                Map<String, Object> map = new HashMap<>();
                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                    map.put(rs.getMetaData().getColumnName(i), rs.getObject(i));
                }
                list.add(map);
            }
        } finally {
            rs.close();
        }
        return list;
    }


    private static <T>  T getMapToEntity(ResultSet rs,Class<T> tClass) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
            map.put(rs.getMetaData().getColumnName(i), rs.getObject(i));
        }

        //设置注解的映射
        DataKeyValue<Field, TableId> dataKeyValue = AnnotationUtil.getAnnotationsField(tClass, TableId.class);
        if (dataKeyValue!=null){
            TableId tableId = dataKeyValue.getValue();
            if (!tableId.value().isEmpty()){
                Object value = map.get(tableId.value());
                //删除掉key
                map.remove(tableId.value());
                //添加新的
                map.put(dataKeyValue.getKey().getName(), value);
            }
        }

        //设置字段的映射
        Map<Field, TableField> annotationsFieldMap = AnnotationUtil.getAnnotationsFieldMap(tClass, TableField.class);
        for (Map.Entry<Field, TableField> fieldTableFieldEntry : annotationsFieldMap.entrySet()) {
            Field field = fieldTableFieldEntry.getKey();
            TableField value = fieldTableFieldEntry.getValue();
            if (!value.value().isEmpty()) {
                Object value1 = map.get(value.value());
                //删除掉key
                map.remove(value.value());
                //添加新的
                map.put(field.getName(), value1);
            }
        }
        return FastJsonUtil.toObject(map,tClass);
    }

}
